package com.calis.ill.order.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.calis.ill.order.entity.Product;
import com.calis.ill.order.repository.ProductRepository;
import com.calis.ill.order.service.api.ProductService;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service("productService")
public class ProductServiceImpl implements ProductService{
	
	@Autowired
	ProductRepository repo;
	
	ObjectMapper mapper = new ObjectMapper();
	
	@Override
	public void add(String title, String code, Double price, Long stock) {
		
		Product product = new Product();
		product.setTitle(title);
		product.setCode(code);
		product.setPrice(price);
		product.setStock(stock);
		
		product.setCreateTime(new Date());
		
		repo.save(product);
	}

	@Override
	public String getProduct(String code) throws Exception {
		Product product = repo.findByCode(code);
		String rst = mapper.writeValueAsString(product);
		return rst;
	}

	@Override
	public String getAll()  throws Exception{
		List<Product> products = (List<Product>) repo.findAll();
		String rst = mapper.writeValueAsString(products);
		return rst;
	}

}
